//
//  RegisterViewController.m
//  livingDesign
//
//  Created by dlios on 15-2-2.
//  Copyright (c) 2015年 怡居. All rights reserved.
//

#import "RegisterViewController.h"
#import "SetUpViewController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"注册";
    
    //    设置登入页面
    
    UIImage *image1 = [UIImage imageNamed:@"bear.jpg"];
    UIImage *image2 = [UIImage imageNamed:@"registbutton.png"];
    UIImage *image3 = [UIImage imageNamed:@"registbutton.png"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:image1];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 100, 25)];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(50, 190, 100, 25)];
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(50, 230, 100, 25)];
    UILabel *label4 = [[UILabel alloc] initWithFrame:CGRectMake(50, 270, 100, 25)];
    UILabel *label5 = [[UILabel alloc] initWithFrame:CGRectMake(50, 310, 100, 25)];
    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(150, 150, 150, 25)];
    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(150, 190, 150, 25)];
    UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(150, 230, 150, 25)];
    UITextField *textField4 = [[UITextField alloc] initWithFrame:CGRectMake(150, 270, 150, 25)];
    UITextField *textField5 = [[UITextField alloc] initWithFrame:CGRectMake(150, 310, 150, 25)];
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    
    label1.text = @"用户名";
    label2.text = @"密码";
    label3.text = @"确认密码";
    label4.text = @"手机号码";
    label5.text = @"邮箱";
    
    textField1.placeholder = @"请输入用户名";
    textField2.placeholder = @"请输入密码";
    textField3.placeholder = @"再次输入密码";
    textField4.placeholder = @"请输入邮箱";
    textField5.placeholder = @"请输入联系方式";
    
    textField1.tag = 1;
    textField2.tag = 2;
    textField3.tag = 3;
    textField4.tag = 4;
    textField5.tag = 5;
    
    textField1.delegate = self;
    textField2.delegate = self;
    textField3.delegate = self;
    textField4.delegate = self;
    textField5.delegate = self;
    
    textField2.secureTextEntry = YES;
    textField3.secureTextEntry = YES;
    
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField2.borderStyle = UITextBorderStyleRoundedRect;
    textField3.borderStyle = UITextBorderStyleRoundedRect;
    textField4.borderStyle = UITextBorderStyleRoundedRect;
    textField5.borderStyle = UITextBorderStyleRoundedRect;
    
    textField1.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField2.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField3.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField4.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField5.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    [button1 setTitle:@"注册" forState:UIControlStateNormal];
    [button2 setTitle:@"返回" forState:UIControlStateNormal];
    [button3 setTitle:@"检测" forState:UIControlStateNormal];
    
    [button1 setBackgroundImage:image2 forState:UIControlStateNormal];
    [button2 setBackgroundImage:image3 forState:UIControlStateNormal];
    
    button1.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    button2.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    
    button1.frame = CGRectMake(90, 300, image2.size.width / 3, image2.size.height / 2);
    button2.frame = CGRectMake(200, 300, image3.size.width / 3, image3.size.height / 2);
    button3.frame = CGRectMake(300, 150, 50, 20);
    
    [button1 addTarget:self action:@selector(buttonClickedLogin:) forControlEvents:UIControlEventTouchUpInside];
    [button2 addTarget:self action:@selector(buttonClickeddismiss:) forControlEvents:UIControlEventTouchUpInside];
    [button3 addTarget:self action:@selector(buttonClickedfind:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStyleDone target:self action:@selector(buttonClickedLogin:)];
    
    
    
    [self.view addSubview:label1];
    [self.view addSubview:label2];
    [self.view addSubview:label3];
    [self.view addSubview:label4];
    [self.view addSubview:label5];
    [self.view addSubview:textField1];
    [self.view addSubview:textField2];
    [self.view addSubview:textField3];
    [self.view addSubview:textField4];
    [self.view addSubview:textField5];
    //    [self.view addSubview:button1];
    //    [self.view addSubview:button2];
    [self.view addSubview:button3];
    
    [label1 release];
    [label2 release];
    [label3 release];
    [label4 release];
    [label5 release];
    [textField1 release];
    [textField2 release];
    [textField3 release];
    [textField4 release];
    [textField5 release];

    
    
    
    
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    UITextField *textfield1 = (UITextField *)[self.view viewWithTag:1];
    UITextField *textfield2 = (UITextField *)[self.view viewWithTag:2];
    UITextField *textfield3 = (UITextField *)[self.view viewWithTag:3];
    UITextField *textfield4 = (UITextField *)[self.view viewWithTag:4];
    UITextField *textfield5 = (UITextField *)[self.view viewWithTag:5];
    
    if (textField == textfield1) {
        [textfield2 becomeFirstResponder];
    } else if(textField == textfield2){
        [textfield3 becomeFirstResponder];
    } else if(textField == textfield3){
        [textfield4 becomeFirstResponder];
    } else if(textField == textfield4){
        [textfield5 becomeFirstResponder];
    } else {
        [textfield5 resignFirstResponder];
    }
    return YES;
}

- (void)buttonClickedfind:(UIButton *)button{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    UITextField *textfield1 = (UITextField *)[self.view viewWithTag:1];
    NSString *s = [user objectForKey:textfield1.text];
    if (textfield1.text.length == 0) {
        _alert = [[[UIAlertView alloc] initWithTitle:@"结果" message:@"用户名不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil] autorelease];
        _alert.tag = 10;
        [_alert show];
    }else if (s == nil) {
        _alert = [[[UIAlertView alloc] initWithTitle:@"结果" message:@"可以使用" delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil] autorelease];
        [_alert show];
    } else {
        _alert = [[[UIAlertView alloc] initWithTitle:@"结果" message:@"用户名已经存在" delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil] autorelease];
        [_alert show];
    }
}

////////   重点
- (void)buttonClickeddismiss:(UIButton *)button{
    
    SetUpViewController *login =[[SetUpViewController alloc] init];
    [self presentViewController:login animated:YES completion:^{
        
    }];
    [login release];
}

- (void)buttonClickedLogin:(UIButton *)button{
    UITextField *textfield1 = (UITextField *)[self.view viewWithTag:1];
    UITextField *textfield2 = (UITextField *)[self.view viewWithTag:2];
    UITextField *textfield3 = (UITextField *)[self.view viewWithTag:3];
    UITextField *textfield4 = (UITextField *)[self.view viewWithTag:4];
    UITextField *textfield5 = (UITextField *)[self.view viewWithTag:5];
    if (textfield1.text.length == 0) {
        _alert = [[[UIAlertView alloc] initWithTitle:@"结果" message:@"用户名不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil] autorelease];
        _alert.tag = 10;
        [_alert show];
    } else if(textfield2.text.length == 0 || textfield3.text.length == 0){
        _alert = [[[UIAlertView alloc] initWithTitle:@"结果" message:@"密码或者确认密码不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil] autorelease];
        _alert.tag = 20;
        [_alert show];
    } else if(![textfield3.text isEqualToString:textfield2.text]){
        _alert = [[[UIAlertView alloc] initWithTitle:@"结果" message:@"密码与确认密码不一致" delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil] autorelease];
        _alert.tag = 30;
        [_alert show];
    } else if(textfield4.text.length == 0){
        _alert = [[[UIAlertView alloc] initWithTitle:@"结果" message:@"邮箱不能为空" delegate:self cancelButtonTitle:nil otherButtonTitles:@"好的", nil] autorelease];
        _alert.tag = 40;
        [_alert show];
    } else if(textfield5.text.length == 0){
        _alert = [[[UIAlertView alloc] initWithTitle:@"结果" message:@"联系方式不能为空" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"好的", nil] autorelease];
        _alert.tag = 50;
        [_alert show];
    } else {
        _alert = [[[UIAlertView alloc] initWithTitle:@"结果" message:@"注册成功" delegate:self cancelButtonTitle:@"立即登录" otherButtonTitles:@"欢迎加入 你家我来装", nil] autorelease];
        _alert.tag = 100;
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:textfield1.text forKey:textfield1.text];
        [user setObject:textfield2.text forKey:[NSString stringWithFormat:@"%@password",textfield1.text]];
        [_alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    UITextField *textfield1 = (UITextField *)[self.view viewWithTag:1];
    UITextField *textfield2 = (UITextField *)[self.view viewWithTag:2];
    UITextField *textfield3 = (UITextField *)[self.view viewWithTag:3];
    UITextField *textfield4 = (UITextField *)[self.view viewWithTag:4];
    UITextField *textfield5 = (UITextField *)[self.view viewWithTag:5];
    if (alertView.tag == 10) {
        [textfield1 becomeFirstResponder];
    } else if (alertView.tag == 20){
        [textfield2 becomeFirstResponder];
    } else if (alertView.tag == 30){
        [textfield3 becomeFirstResponder];
    } else if (alertView.tag == 40){
        [textfield4 becomeFirstResponder];
    } else if (alertView.tag == 50){
        [textfield5 becomeFirstResponder];
    } else if (alertView.tag == 100){
        if (buttonIndex == 0){
            UIViewController *vc = [self.navigationController.viewControllers objectAtIndex:0];
            [self.navigationController popToViewController:vc animated:YES];
        } else {
            
        }
    }
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITextField *textfield1 = (UITextField *)[self.view viewWithTag:1];
    UITextField *textfield2 = (UITextField *)[self.view viewWithTag:2];
    UITextField *textfield3 = (UITextField *)[self.view viewWithTag:3];
    UITextField *textfield4 = (UITextField *)[self.view viewWithTag:4];
    UITextField *textfield5 = (UITextField *)[self.view viewWithTag:5];
    [textfield1 resignFirstResponder];
    [textfield2 resignFirstResponder];
    [textfield3 resignFirstResponder];
    [textfield4 resignFirstResponder];
    [textfield5 resignFirstResponder];
}


- (void)dealloc{
    [super dealloc];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
